home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / TVTOOLS.ZIP / INDOUBLE.CPP < prev    next >
C/C++ Source or Header  |  1993-10-19  |  3KB  |  115 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #define Uses_TStreamable
  4. #define Uses_MsgBox
  5. #define Uses_TInputDouble
  6. #include "tvtools.h"
  7. __link( RInputLine )
  8.  
  9.  
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <values.h>
  13. #include <stdlib.h>
  14. #include <strstream.h>
  15.  
  16.  
  17.  
  18.  
  19. // TInputDouble
  20.  
  21. const char * const TInputDouble::name = "TInputDouble";
  22.  
  23. void TInputDouble::write( opstream& os )
  24. {
  25.  TInputLine::write( os );
  26.  os << min;
  27.  os << max;
  28. }
  29.  
  30. void *TInputDouble::read( ipstream& is )
  31. {
  32.  TInputLine::read( is );
  33.  is >> min;
  34.  is >> max;
  35.  return this;
  36. }
  37.  
  38. TStreamable *TInputDouble::build()
  39. {
  40.  return new TInputDouble( streamableInit );
  41. }
  42.  
  43.  
  44. TStreamableClass RInputDouble( TInputDouble::name,
  45.                                TInputDouble::build,
  46.                                __DELTA(TInputDouble)
  47.                              );
  48.  
  49. TInputDouble::TInputDouble( const TRect& bounds,
  50.                 int aMaxLen,
  51.                 double aMin,
  52.                 double aMax
  53.                           )
  54.              :TInputRegExp( bounds, aMaxLen, "-.0-9eE" )
  55. {
  56.  min = aMin;
  57.  max = aMax;
  58. }
  59.  
  60. TInputDouble::TInputDouble( int x, int y,
  61.                 int aMaxLen,
  62.                 double aMin,
  63.                 double aMax
  64.                           )
  65.              :TInputRegExp( x, y, aMaxLen, "-.0-9eE" )
  66. {
  67.  min = aMin;
  68.  max = aMax;
  69. }
  70.  
  71.  
  72. ushort TInputDouble::dataSize()
  73. {
  74.  return sizeof( double );
  75. }
  76.  
  77. void TInputDouble::getData( void *rec )
  78. {
  79.  *(double *)rec = atof( data );
  80. }
  81.  
  82. void TInputDouble::setData( void *rec )
  83. {
  84.  char buffer[256];
  85.  sprintf( buffer, "%G", *(double *)rec );
  86.  strncpy( data, buffer, maxLen );
  87.  data[maxLen] = EOS;
  88.  selectAll(True);
  89. }
  90.  
  91. Boolean TInputDouble::valid( ushort command )
  92. {
  93.   switch( command )
  94.         {
  95.           case cmReleasedFocus: return True;
  96.  
  97.           case cmQuit :
  98.           case cmClose:
  99.           case cmOK   : double value = atof( data );
  100.                         if ( (! *data) || (value < min) || (value > max) )
  101.                            {
  102.                              select();
  103.                              messageBox( mfError | mfOKButton,
  104.                                          "\03Number must be between %G and %G.",
  105.                                          min, max
  106.                                        );
  107.                              selectAll( True );
  108.                              return False;
  109.                            }
  110.                         break;
  111.         }
  112.  
  113.   return TInputRegExp::valid( command );
  114. }
  115.